import React from 'react';
import {connect} from 'react-redux';
import {Switch, Route, Redirect} from 'react-router-dom';

/*IMPORT COMPONENT*/
import Login from './person/Login';
import Info from './person/Info';
import Tip from './person/Tip';

/*IMPORT API*/
import {checkLogin} from '../api/person';
import '../static/css/person.less';

/*RENDER*/
class Person extends React.Component {
    constructor(props, context) {
        super(props, context);

        //=>STATE
        this.state = {
            isLogin: false
        };
    }

    //=>Verify login
    async componentWillMount() {
        let result = await checkLogin(),
            isLogin = parseFloat(result.code) === 0 ? true : false;
        this.setState({isLogin});
    }

    async componentWillReceiveProps() {
        let result = await checkLogin(),
            isLogin = parseFloat(result.code) === 0 ? true : false;
        this.setState({isLogin});
    }

    render() {
        return <section>
            <Switch>
                <Route path='/person/info' render={() => {
                    if (this.state.isLogin) {
                        return <Info/>;
                    }
                    return <Tip/>;
                }}/>
                <Route path='/person/login' component={Login}/>
                <Redirect from='/person' to='/person/info'/>
            </Switch>
        </section>;
    }
}

export default connect()(Person);